package demo1;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;

class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


//    public int compareTo(Student o) {
//        return 0;
//    }
}
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student a,Student b) {
        return a.age-b.age;
    }
}
class NameComparator implements Comparator<Student>
{
    @Override
    public int compare(Student a,Student b) {
        return a.name.compareTo(b.name);
    }
}
public class Text {
//    public static void  mysort(Comparable[] comparable){
//        for (int i = 0; i < comparable.length; i++) {
//            for (int j = 0; j < comparable.length-1-i; j++) {
//                if(comparable[j].compareTo(comparable[j+1])>0){
//                    Comparable tem=comparable[j];
//                    comparable[j]=comparable[j+1];
//                    comparable[j+1]=tem;
//                }
//            }
//        }
//    }
public static void main(String[] args) {
  Student  students1=new Student("a",13);
  Student  students2=new Student("b",10);
    AgeComparator ageComparator = new AgeComparator();
    System.out.println(ageComparator.compare(students1, students2));
    NameComparator nameComparator = new NameComparator();
    System.out.println(nameComparator.compare(students1, students2));
    Student[]students=new Student[3];
    students[0]=new Student("b",11);
    students[1]=new Student("z",10);
    students[2]=new Student("c",99);
    Arrays.sort(students,new NameComparator());
    System.out.println(Arrays.toString(students));
    Arrays.sort(students,new AgeComparator());
    System.out.println(Arrays.toString(students));
}
    public static void main3(String[] args) {
        Student[]students=new Student[3];
        students[0]=new Student("b",11);
        students[1]=new Student("z",10);
        students[2]=new Student("c",99);
//        Arrays.sort(students);
//        mysort(students);
        System.out.println(Arrays.toString(students));
    }
//    public static void main1(String[] args) {
//        Student student1=new Student("ba",0);
//        Student student2=new Student("b",10);
//        if(student1.compareTo(student2)>0){
//            System.out.println("张三大");
//        }
//        else {
//            System.out.println("李四大");
//        }
//        if(student1.compareTo(student2)>0){
//            System.out.println("张三大");
//        }
//        else {
//            System.out.println("李四大");
//        }
//    }
}
//package demo1;
//
//import java.lang.reflect.Array;
//import java.util.Arrays;
//import java.util.Comparator;
//
//class Student implements Comparable<Student>{
//    public String name;
//    public int age;
//
//    public Student(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
//    public int compareTo(Student o) {
//        return this.age-o.age;
//    }
//}
//public class Text {
//    public static void main(String[] args) {
//        Student[]students=new Student[3];
//        students[0]=new Student("b",11);
//        students[1]=new Student("z",10);
//        students[2]=new Student("c",99);
//        Arrays.sort(students);
//        System.out.println(Arrays.toString(students));
//    }
//}
